package com.jingli.pile.util;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.spec.KeySpec;
public class HMacMd5KeySpec implements KeySpec{
    private static final int LEN = 64;

    ByteBuffer buf = ByteBuffer.allocate(LEN);

    public HMacMd5KeySpec(String key){
        this(key.getBytes(StandardCharsets.ISO_8859_1));
    }

    public HMacMd5KeySpec(byte[] key){
        buf.position(0);
        buf.limit(buf.capacity());

        if(key.length>LEN){
            key = Md5Utils.md5Bytes(key);
        }

        buf.put(key);

        buf.flip();
        buf.rewind();

    }

    public static KeySpec of(String s) {
        return new HMacMd5KeySpec(s);
    }

    public byte[] toBytes(){
        return buf.array();
    }
}
